<?php

namespace app\repository;

use app\popo\model\Order;
use Webman\Http\Request;

class OrderRepository
{
    public function conditionSearch(Request $request): array
    {
        $page = (int)$request->get('page', 1);
        $pageSize = (int)$request->get('pageSize', 10);
        $number = trim($request->get('number', ''));
        $phone = trim($request->get('phone', ''));
        $status = $request->get('status');
        $beginTime = $request->get('beginTime');
        $endTime = $request->get('endTime');
        $query = Order::query();

        // 时间区间
        if ($beginTime) {
            $query->where('order_time', '>=', $beginTime);
        }
        if ($endTime) {
            $query->where('order_time', '<=', $endTime);
        }

        // 订单号模糊
        if ($number) {
            $query->where('number', 'like', "%{$number}%");
        }

        // 手机号精确
        if ($phone) {
            $query->where('phone', $phone);
        }

        // 状态
        if ($status !== null && $status !== '') {
            $query->where('status', (int)$status);
        }

        // 排序：最新订单在前
        $query->orderByDesc('id');

        // 分页
        $total = $query->count();
        $records = $query->forPage($page, $pageSize)->get()->map(function (Order $order) {
            return [
                "id" => $order->id,
                "number" => $order->number,
                "status" => $order->status,
                "userId" => $order->user_id,
                "addressBookId" => $order->address_book_id,
                "orderTime" => $order->order_time ? $order->order_time->format('Y-m-d H:i:s') : null,
                "checkoutTime" => $order->checkout_time ? $order->checkout_time->format('Y-m-d H:i:s') : null,
                "payMethod" => $order->pay_method,
                "payStatus" => $order->pay_status,
                "amount" => $order->amount,
                "remark" => $order->remark,
                "userName" => $order->user_name,
                "phone" => $order->phone,
                "address" => $order->address,
                "consignee" => $order->consignee,
                "cancelReason" => $order->cancel_reason,
                "rejectionReason" => $order->rejection_reason,
                "cancelTime" => $order->cancel_time ? $order->cancel_time->format('Y-m-d H:i:s') : null,
                "estimatedDeliveryTime" => $order->estimated_delivery_time ? $order->estimated_delivery_time->format('Y-m-d H:i:s') : null,
                "deliveryStatus" => $order->delivery_status,
                "deliveryTime" => $order->delivery_time ? $order->delivery_time->format('Y-m-d H:i:s') : null,
                "packAmount" => $order->pack_amount,
                "tablewareNumber" => $order->tableware_number,
                "tablewareStatus" => $order->tableware_status,
                "orderDishes" => []
            ];
        });

        return compact("total", "records");
    }
}